package com.code;

import com.linux.huhx.utils.RegrexUtils;
import org.apache.commons.io.FileUtils;
import org.junit.Test;
import org.springframework.util.StringUtils;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GenerateUtil {

    /**
     * 简单的字符串替换函数
     */
    public static void simpleReplaceTemplate(String resource, String filepath, Map<Integer, Integer> transFunc) {
        if (StringUtils.isEmpty(filepath)) {
            filepath = Constants.DEFAULT_FILE_PATH;
        }
        File file = new File(filepath);
        try {
            List<String> stringList = FileUtils.readLines(file);
            if (transFunc == null || transFunc.size() < 1) {
                for (String string : stringList) {
                    String[] originStrArray = string.split("\\s");
                    String line = RegrexUtils.messageFormat(resource, originStrArray);
                    System.out.println(line);
                }
                return;
            }
            for (String string : stringList) {
                String[] originStrArray = string.split("\\s");
                for (Integer index : transFunc.keySet()) {
                    Integer funcNumber = transFunc.get(index);
                    switch (funcNumber) {
                        case Constants.FIRST_LETTER_UPPER:
                            originStrArray[index] = CodeToolUtils.transforFirstLetterUpper(originStrArray[index]); // 首字母大写
                            break;
                        case 2:
                            originStrArray[index] = CodeToolUtils.transforFirstLetterLower(originStrArray[index]); // 首字母小写
                            break;
                        case 3:
                            originStrArray[index] = CodeToolUtils.getWithHumpContent(originStrArray[index]); // 驼峰式写法
                            break;
                        default:
                            break;
                    }
                }
                String line = RegrexUtils.messageFormat(resource, originStrArray);
                System.out.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void simpleReplaceTemplate(String resource) {
        simpleReplaceTemplate(resource, null, null);
    }

    public static void simpleReplaceTemplate(String resource, Map<Integer, Integer> transFunc) {
        simpleReplaceTemplate(resource, null, transFunc);
    }

    @Test
    public void test_1() {
        String requestData = "<prop key=\"{0}.do\">{0}Action</prop> <!-- {1} -->";
        Map<Integer, Integer> integerMap = new HashMap<>();
        integerMap.put(0, Constants.FIRST_LETTER_UPPER);
        GenerateUtil.simpleReplaceTemplate(requestData, integerMap);
    }
}
